﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GFluentCode.Entity;

namespace GFluentCode.CodeMaker
{
    public partial class UcTable : UserControl
    {
        GFluentTableEntity _table;

        public string TableName => GbTable.Text;

        public Action<UcTable,string> DoubleClicked;

        public Action<GFluentColumnEntity,bool> PropertyChecked;

        public UcTable(GFluentTableEntity table)
        {
            InitializeComponent();

            _table = table;

            GbTable.Text = table.TableName;

            DgvProperty.AutoGenerateColumns = false;
            DgvProperty.DataSource = table.Columns;
        }

        private void DgvProperty_DoubleClick(object sender, EventArgs e)
        {
            DoubleClicked?.Invoke(this,TableName);
        }

        private void DgvProperty_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.ColumnIndex != 0) return;

            var ds = DgvProperty.DataSource as List<GFluentColumnEntity>;

            var chk = DgvProperty.Rows[e.RowIndex].Cells[0].EditedFormattedValue.ToString();

            PropertyChecked?.Invoke(ds[e.RowIndex], bool.Parse(chk));
        }
    }
}
